
import { gameHotUpdateMgr } from "./GameHotUpdateMgr"
import { UIMgr } from '../../lib/UIMgr';
const { ccclass, property } = cc._decorator;

@ccclass
export default class GamehotUpdata1 extends cc.Component {
	@property(cc.ProgressBar)
	progressBar: cc.ProgressBar = null; //进度条;

	@property(cc.Node)
	bg: cc.Node = null;
	@property(cc.Label)
	tips: cc.Label = null;

	file_module: string = "";
	listen_event: any = null;
	updir: any = null;
	file_version: any = null;
	file_project: any = null;
	hotup: any = null;
	newversion: String = null;
	updataing: boolean = false;

	ALREADY_UP_TO_DATEevent = null;
	UPDATE_FINISHEDEevent = null;
	UPDATE_FAILEDEevent = null;
	UPDATE_ERROREevent = null;
	UPDATE_PROGRESSIONEevent = null;
	UPDATE_VERSIONEevent = null;
	NEW_VERSION_FOUNDEevent = null;

	callBack = null;
	gameid: number = 0;

	onLoad() {
		this.progressBar.progress = 0.0;
		this.tips.string = "";
		this.ALREADY_UP_TO_DATEevent = cc.vv.eventCustom.on(
			"ALREADY_UP_TO_DATE",
			this.ALREADY_UP_TO_DATE.bind(this),
			this.node
		);
		this.UPDATE_FINISHEDEevent = cc.vv.eventCustom.on(
			"UPDATE_FINISHED",
			this.UPDATE_FINISHED.bind(this),
			this.node
		);
		this.UPDATE_FAILEDEevent = cc.vv.eventCustom.on(
			"UPDATE_FAILED",
			this.UPDATE_FAILED.bind(this),
			this.node
		);
		this.UPDATE_ERROREevent = cc.vv.eventCustom.on(
			"UPDATE_ERROR",
			this.UPDATE_ERROR.bind(this),
			this.node
		);
		this.UPDATE_PROGRESSIONEevent = cc.vv.eventCustom.on(
			"UPDATE_PROGRESSION",
			this.UPDATE_PROGRESSION.bind(this),
			this.node
		);
		this.UPDATE_VERSIONEevent = cc.vv.eventCustom.on(
			"UPDATE_VERSION",
			this.UPDATE_VERSION.bind(this),
			this.node
		);
		this.NEW_VERSION_FOUNDEevent = cc.vv.eventCustom.on(
			"NEW_VERSION_FOUND",
			this.NEW_VERSION_FOUND.bind(this),
			this.node
		);
		//this.node.on("click", this.onClick, this);
	}

	onDestroy() {
		if (typeof this.ALREADY_UP_TO_DATEevent != "undefined") {
			cc.vv.eventCustom.off("ALREADY_UP_TO_DATE", this.ALREADY_UP_TO_DATEevent);
			this.ALREADY_UP_TO_DATEevent = undefined;
		}
		if (typeof this.UPDATE_FINISHEDEevent != "undefined") {
			cc.vv.eventCustom.off("UPDATE_FINISHED", this.UPDATE_FINISHEDEevent);
			this.UPDATE_FINISHEDEevent = undefined;
		}

		if (typeof this.UPDATE_FAILEDEevent != "undefined") {
			cc.vv.eventCustom.off("UPDATE_FAILED", this.UPDATE_FAILEDEevent);
			this.UPDATE_FAILEDEevent = undefined;
		}

		if (typeof this.UPDATE_ERROREevent != "undefined") {
			cc.vv.eventCustom.off("UPDATE_ERROR", this.UPDATE_ERROREevent);
			this.UPDATE_ERROREevent = undefined;
		}


		if (typeof this.UPDATE_PROGRESSIONEevent != "undefined") {
			cc.vv.eventCustom.off("UPDATE_PROGRESSION", this.UPDATE_PROGRESSIONEevent);
			this.UPDATE_PROGRESSIONEevent = undefined;
		}

		if (typeof this.UPDATE_VERSIONEevent != "undefined") {
			cc.vv.eventCustom.off("UPDATE_VERSION", this.UPDATE_VERSIONEevent);
			this.UPDATE_VERSIONEevent = undefined;
		}

		if (typeof this.NEW_VERSION_FOUNDEevent != "undefined") {
			cc.vv.eventCustom.off("NEW_VERSION_FOUND", this.NEW_VERSION_FOUNDEevent);
			this.NEW_VERSION_FOUNDEevent = undefined;
		}
	}

	init(args) {
		this.bg.active = false;
		this.gameid = args[0];
		this.callBack = args[1];
		this.file_module = cc.vv.GLDefine.getEnglishNameById(this.gameid);
		this.check_update(this.file_module);

	}

	check_update(mod) {
		this.updir = cc.vv.GLConfig.resPath + "/" + this.file_module;
		this.file_module = mod || "";
		this.file_version = "version.manifest";
		this.file_project = "project.manifest";
		if (this.file_module != "") {
			this.file_version = this.file_module + "_version.manifest";
			this.file_project = this.file_module + "_project.manifest";
		}
		let file_manifest = "";
		let temp =
			jsb.fileUtils.getWritablePath() + this.updir + "/" + this.file_project;
		if (jsb.fileUtils.isFileExist(temp))
			file_manifest = jsb.fileUtils.getStringFromFile(temp);
		else if (mod == "" || mod == "hall")
			//大厅或单包，请确认你的包体内包含资源列表配置文件
			file_manifest = jsb.fileUtils.getStringFromFile(this.file_project);

		this.check_before();
		//this.hotup && this.hotup.release();
		let d = {
			file_version: this.file_version,
			file_project: this.file_project,
			file_module: this.file_module,
			file_manifest: file_manifest || ""
			//_version : this.file_version
		};
		this.hotup = gameHotUpdateMgr.newUpdate(mod, d);
		if (this.hotup && this.hotup.isupdating)  //正在更新中的游戏
		{
			this.bg.active = true;

			return
		}
		if (this.hotup && this.hotup.getQueueStatus()) {//在更新队列中排队的游戏
			this.bg.active = true;
			this.tips.string = "等待中";
			return
		}
		if (this.hotup && this.hotup.getneedupdataStatus())  //检查过更新 需要更新 没有在队列中的游戏
		{
			this.bg.active = true;
			cc.vv.eventCustom.emit("NEW_VERSION_FOUND", {  // 发送到GameChoose.js
				moduleTmp: mod
			});
			return;
		}
		if (this.hotup && this.hotup.getisupdataSucessStatus())  //已经更新成功的游戏
		{
			this.bg.active = false;
			cc.vv.eventCustom.emit("ALREADY_UP_TO_DATE", {
				moduleTmp: mod
			});
			return;
		}
		if (this.hotup && this.hotup.getisfailStatus())//更新失败的游戏
		{
		} else {
			//以上条件都不满足
			this.bg.active = true;
			this.hotup.check_update("http://" + cc.vv.GLConfig.hotUpdateURL + "/", this.updir);
		}
	}

	//更新前检测旧版本目录 和 本地缓存目录是否存在未完成的更新
	check_before() {
		let path = jsb.fileUtils.getWritablePath() + this.updir + "_temp";
		let file = path + "/project.manifest.temp";
		if (jsb.fileUtils.isFileExist(file)) {
			let str = jsb.fileUtils.getStringFromFile(file);
			if (str == "") str = "{}";
			let mod = JSON.parse(str).module;
			if (mod != this.file_module)
				console.log("remove temp file:" + jsb.fileUtils.removeFile(file));
		}
	}

	//更新完成后检测本地manifest，进行更名等操作
	check_after(finish) {
		if (!finish) {
			// 更新失败后 从管理队列中移除
			gameHotUpdateMgr.removeUpdate(this.file_module);
			this.bg.active = false;
			this.tips.string = "";

			//检查是否需要更新
			this.check_update(this.file_module);
			cc.log("更新失败后结束了");
			return;
		}
		console.log("更新成功 manifest文件已经成功更新: ");
	}
	NEW_VERSION_FOUND(detail) {
		//检测到有更新
		if (detail[0].moduleTmp != this.file_module) {
			return;
		}
		this.newversion = detail[0].version;

		gameHotUpdateMgr.insertToQueue(this.hotup);
		//this.hotIcon.active = true;
		// this.txt_version.string =
		//   "检测到新版本" + this.newversion + ",点击开始更新";
	}
	UPDATE_PROGRESSION(detail) {

		let moduleTmp = detail[0].moduleTmp;
		if (moduleTmp != this.file_module) {
			return;
		}
		this.tips.string = "";
		this.updataing = true;
		let event = detail[0].event;
		// console.log('upgrade ---:' + (event ? event.getMessage() : ''))
		let p = event ? event.getPercent() : null;
		if (p) {
			if (p >= 1) p = 1;

			this.progressBar.progress = p;
			this.tips.string = "下载进度:" + (100 * p).toFixed(2) + "%";
		}
	}

	ALREADY_UP_TO_DATE(detail) {
		let moduleTmp = detail[0].moduleTmp;
		if (moduleTmp != this.file_module) {
			return;
		}
		// this.txt_process.string = "更新完成"; //，正在加载资源...'
		this.check_after(true);
		this.updateOver();
	}

	UPDATE_FINISHED(detail) {
		let moduleTmp = detail[0].moduleTmp;
		if (moduleTmp != this.file_module) {
			return;
		}
		// this.txt_process.string = "更新完成"; //正在加载资源...'
		let path = "Game/" + this.file_module + "/" + this.file_module;
		this.releaseRe(path);
		this.check_after(true);
		this.updateOver();

	}

	UPDATE_FAILED(detail) {
		let moduleTmp = detail[0].moduleTmp;
		if (moduleTmp != this.file_module) {
			return;
		}
		// this.txt_process.string = "更新完成，正在加载资源2...";
		this.check_after(false);
		if (this.file_module == "hall") {
			console.log("game restart2");
			cc.game.restart();
		}
	}

	UPDATE_ERROR(detail) {
		let moduleTmp = detail[0].moduleTmp;
		if (moduleTmp != this.file_module) {
			return;
		}
		this.check_after(false);
		console.log("update error ---------------------1111");
	}

	UPDATE_VERSION(detail) {
		let version = detail[0].version;
		let moduleTmp = detail[0].moduleTmp;
		if (moduleTmp != this.file_module) {
			return;
		}
		// this.txt_version.string = version;
		// console.log("version is " + version + " mod = %s " + moduleTmp);
	}
	updateOver() {
		console.log("更新完成了,刷新ui");
		this.updataing = false;
		this.node.active = false;
		if (this.callBack) {
			this.callBack();
		}
		cc.vv.UIMgr.close(cc.vv.GLConfig.gameHotUpdate1_path, cc.vv.UIMgr.closeType.destory);
	}
	releaseRe(path) {
		//强制释放一次
		cc.vv.UIMgr.forceRelease(path);
	}
}
